package com.th.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.th.common.domain.UsTask;
import com.th.common.domain.UsTaskRequire;
import com.th.common.vo.UsTaskVo;
import com.th.config.web.BaseController;
import com.th.service.UsTaskRequireService;
import com.th.service.UsTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ValcanoZz
 * @creat 2023-06-23-9:18
 */

@Slf4j
@RestController
@RequestMapping("/api/task")
@Api(tags = "任务接口")
public class UsTaskController extends BaseController {

    @Autowired
    private UsTaskService usTaskService;

    @Autowired
    private UsTaskRequireService usTaskRequireService;

    @GetMapping("/getTaskOne")
    @ApiOperation("查看某个订单所属的任务信息")
    public List<UsTaskVo> getTaskOne(Long orderId) {
        List<UsTask> usTasks = usTaskService.selectTasksByOrderId(orderId);
        return getVoByTasks(usTasks);
    }


    @GetMapping("/getPage")
    @ApiOperation("查看所有任务信息")
    public List<UsTaskVo> getPage(int startPage, int size) {
        List<UsTask> usTasks = usTaskService.selectPage(startPage, size);
        return getVoByTasks(usTasks);
    }

    @GetMapping("/getPageByConditions")
    @ApiOperation("分页查询: 可以根据任务的状态，接收的设计师查询，根据需求的年龄区间，男女装，衣服类型，最大成本，设计师等级查询")
    public List<UsTaskVo> selectByConditions(
            Integer startPage, Integer size,
            Integer status, Integer designerId,
            Integer minAge, Integer maxAge, Integer gender, Integer type, Integer cost, Integer level
    ) {
        List<UsTaskVo> list = new ArrayList<>();

        LambdaQueryWrapper<UsTask> taskWrapper = new LambdaQueryWrapper<>();
        taskWrapper.eq(status != null, UsTask::getStatus, status);
        taskWrapper.eq(designerId != null, UsTask::getDesignerId, designerId);
        List<UsTask> tasks = usTaskService.selectPage(startPage, size, taskWrapper);    // 查到符合条件的任务

        // 在查符合条件的需求，并且这些需求是属于符合条件的任务的

        for (UsTask task : tasks) {
            LambdaQueryWrapper<UsTaskRequire> requireWrapper = new LambdaQueryWrapper<>();
            requireWrapper.ge(minAge != null, UsTaskRequire::getMinAge, minAge);
            requireWrapper.le(maxAge != null, UsTaskRequire::getMinAge, minAge);
            requireWrapper.eq(gender != null && (gender == 1 || gender == 0), UsTaskRequire::getGender, gender);
            requireWrapper.eq(type != null && type >= 0 && type <= 5, UsTaskRequire::getTypes, type);
            requireWrapper.le(cost != null, UsTaskRequire::getCost, cost);
            requireWrapper.ge(level != null, UsTaskRequire::getLevel, level);
            requireWrapper.eq(task.getId() != null, UsTaskRequire::getTaskId, task.getId());
            UsTaskVo vo = new UsTaskVo();
            vo.setUsTask(task);
            vo.setRequires(usTaskRequireService.selectRequiresByTask(requireWrapper));
            list.add(vo);
        }
        return list;
    }

    private List<UsTaskVo> getVoByTasks(List<UsTask> usTasks) {
        List<UsTaskVo> list = new ArrayList<>();
        for (UsTask task : usTasks) {
            List<UsTaskRequire> requires = usTaskRequireService.selectRequiresByTask(task.getId());
            UsTaskVo usTaskVo = new UsTaskVo();
            usTaskVo.setUsTask(task);
            usTaskVo.setRequires(requires);
            list.add(usTaskVo);
        }
        return list;
    }

//    @PostMapping("/submit")
//    @ApiOperation("顾问提交任务信息")
//    public boolean submitTask(@RequestBody UsTaskVo usTaskVo) {
//        UsTask usTask = usTaskVo.getUsTask();
//        List<UsTaskRequire> requires = usTaskVo.getRequires();
//        Long taskId = usTaskService.insertUsTask(usTask);
//        for (UsTaskRequire require : requires) {
//            require.setTaskId(taskId);
//        }
//        return usTaskRequireService.insertRequires(requires);
//    }
//
//    @PutMapping("/updateTask")
//    public Boolean updateTask(@RequestBody UsTaskVo usTaskVo) {
//        UsTask usTask = usTaskVo.getUsTask();
//        List<UsTaskRequire> requires = usTaskVo.getRequires();
//        usTaskService.updateTask(usTask);
//        usTaskRequireService.updateRequires(requires);
//        return true;
//    }
//
//    @PutMapping("/updateRequire")
//    public Boolean updateTask(@RequestBody UsTaskRequire usTaskRequire) {
//        usTaskRequireService.updateOne(usTaskRequire);
//        return true;
//    }
//
//    @DeleteMapping("/deleteTask")
//    public Boolean deleteTask(Integer taskId) {
//        usTaskService.deleteTask(taskId);
//        usTaskRequireService.deleteRequireByTaskId(taskId);
//        return true;
//    }
//
//    @DeleteMapping("/deleteRequire")
//    public Boolean deleteRequire(Integer requireId) {
//        usTaskRequireService.deleteRequire(requireId);
//        return true;
//    }

}
